<?php
	include('class/db.class.php');

	$db = new DB();
	/* 查询数据 */
	$get_rt = $db->get_all('select * from test');
	
	/* 修改数据 */
	$id = $_GET['edit'];
	if($id){
		$get_update = $db->get_one('select * from test where id =' .$id);
		if($_POST['edit_sub']){
			$xueli = $_POST['xueli'];
			$update = $db->update('test',array(xueli=>"$xueli"),'id =' .$id);
			echo "<script>alert('修改成功!');window.location.href='index.php'</script>"; 
		}
	}
	
	/* 添加数据 */
	if($_POST['submit']){
		$name = $_POST['name'];
		$xueli = $_POST['xueli'];
		$age = $_POST['age'];
		$insert = $db->insert('test',array(name=>"$name",xueli=>"$xueli",age=>"$age"));
		echo "<script>alert('添加成功!');window.location.href='index.php'</script>"; 
	}
	
	/* 删除数据 */
	$ids = $_GET['del'];
	if($ids){
		$delete = $db->delete('test','id =' .$ids);
		echo "<script>alert('删除成功!');window.location.href='index.php'</script>";
	}
	
	/* 修改数据 
	$id = $_GET['edit'];
	$xueli = $_POST['xueli'];
	if($id){
		$sql_sel = "select * from test where id = $id";
		$q = mysql_query($sql_sel);
		$rows = mysql_fetch_array($q);
		
		if($_POST['edit_sub']){
			$sql_edit = "update test set xueli = '$xueli' where id = $id"; 
			$row = mysql_query($sql_edit);
			if($row){
				echo "<script>alert('修改成功!');window.location.href='index.php'</script>"; 
			}else{
				echo "<script>alert('修改失败!');</script>"; 
			}
		}
	}*/
	
	/* 删除数据 
	$uid = $_GET['del'];
	if($uid){
		$sql_del = "delete from test  where id = $uid"; 
		$res = mysql_query($sql_del);
		if($res){
			echo "<script>alert('删除成功!');window.location.href='index.php'</script>"; 
		}else{
			echo "<script>alert('删除失败!');</script>"; 
		}
	}*/
	
	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
</head>
<style type="text/css">
ul{ list-style:none;}
li{ line-height:25px; height:25px; margin:5px 0;}
.fl{float:left;}
.fr{float:right;}
.w500{width:50%;}
a{ text-decoration:none; margin:0 5px;}
</style>
<body>
<div class="fl w500">
<?php if($id==''){?>
    <form action="" method="post" id="form1">
        <ul>
            <li>姓名:<input type="text" name="name" /></li>
            <li>学历:<input type="text" name="xueli" /></li>
            <li>年龄:<input type="text" name="age" /></li>
            <li><input type="submit" value="提交数据" name="submit" /></li> 
        </ul>
    </form>
<?php }else{?>
	<form action="" method="post" id="form1">
        <ul>
            <li>姓名:<?php echo $get_update['name']?></li>
            <li>学历:<input type="text" name="xueli" value="<?php echo $get_update['xueli']?>" /></li>
            <li>年龄:<?php echo $get_update['age']?></li>
            <li><input type="submit" value="修 改" name="edit_sub" />&nbsp;<input type="button" value="返 回" onclick="window.location.href='index.php'" /></li> 
        </ul>
    </form>
<?php }?>
</div> 
<div class="fl w500">
	<table>
    	<tr>
        	<td width="50" align="center">姓名</td>
            <td width="50" align="center">学历</td>
            <td width="50" align="center">年龄</td>
            <td width="100" align="center">操作</td>
        </tr>
        <?php if($get_rt){?>
			<?php foreach($get_rt as $value){?>
            <tr>
                <td align="center"><?php echo $value['name']?></td>
                <td align="center"><?php echo $value['xueli']?></td>
                <td align="center"><?php echo $value['age']?></td>
                <td align="center"><a href="index.php?edit=<?php echo $value['id']?>">修改</a><a href="index.php?del=<?php echo $value['id']?>" onclick="if(confirm('是否删除数据?')){return true}else{return false}">删除</a></td>
            </tr>
            <?php }?>
        <?php }else{?>
        	<tr><td colspan="4" align="center">暂无数据,请添加信息!</td></tr>
        <?php }?>
    </table>
</div> 
</body>
</html>
